package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pojo.Article;
import pojo.Collection;
import pojo.Comment;
import pojo.User;
import service.IArticleService;
import service.ICollectionService;
import service.ICommentService;
import util.UploadFileUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class GeneralAPI {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private ICollectionService collectionService;
    @Autowired
    private ICommentService commentService;
    /**
     * 图片上传
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/upload")
    private Map uploadImage(MultipartFile file, HttpServletRequest request){
        //创建应答数据容器
        Map map = new HashMap();
        //设置帖子图片的保存路径
        String uploadPath = "/upload/article";
        //图片保存
        String fileName = UploadFileUtil.upload(file, request, uploadPath);
        if(fileName==null){
            map.put("status",1);
            map.put("message","图片上传失败" );
            return map;
        }
        //设置返回数据
        map.put("status",0 );
        map.put("url", uploadPath+"/"+fileName);
        map.put("fileName", fileName);
        return map;
    }

    /**
     * 收藏查询
     * @param cid
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/collection/find/",method = RequestMethod.POST)
    public Map collectionFind(int cid, HttpServletRequest request){
        Map map = new HashMap();
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return null;
        }
        Collection collection = new Collection();
        collection.setCollectionArticleId(cid);
        collection.setCollectionUserId(user.getUserId());
        collection =  collectionService.getCollectionByMany(collection);
        map.put("status",0 );
        Map data = new HashMap();
        if(collection!=null){
            data.put("collection", user.getUserId());
            map.put("data",data );
            return map;
        }
        map.put("data", data);
        return map;
    }

    /**
     * 添加收藏
     * @param cid
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/collection/add/",method = RequestMethod.POST)
    public Map collectionAdd(int cid, HttpServletRequest request){
        Map map = new HashMap();
        User user = (User) request.getSession().getAttribute("user");
        if(user== null){
            return null;
        }
        Collection collection = new Collection();
        collection.setCollectionArticleId(cid);
        collection.setCollectionUserId(user.getUserId());
        int i = collectionService.addCollection(collection);
        Map data = new HashMap();
        if(i==1){
            map.put("status",0 );
            return map;
        }
        map.put("status", 1);
        map.put("msg","收藏失败" );
        return map;
    }

    /**
     * 取消收藏
     * @param cid
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/collection/remove/",method = RequestMethod.POST)
    public Map collectionRemove(int cid, HttpServletRequest request){
        Map map = new HashMap();
        User user = (User) request.getSession().getAttribute("user");
        if(user== null){
            return null;
        }
        Collection collection = new Collection();
        collection.setCollectionArticleId(cid);
        collection.setCollectionUserId(user.getUserId());
        int i = collectionService.removeCollectionByMany(collection);
        Map data = new HashMap();
        if(i==1){
            map.put("status",0 );
            return map;
        }
        map.put("status", 1);
        map.put("msg","取消收藏失败" );
        return map;
    }

    /**
     * 采纳评论
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api/jieda-accept/",method = RequestMethod.POST)
    @Transactional
    public Map acceptComment(int id){
        Map map = new HashMap();
        //根据id获取该评论信息
        Comment comment = commentService.getCommentById(id);
        //将评论设置为采纳
        comment.setCommentAccept((byte) 1);
        //数据更新
        int i = commentService.modifyCommentByCommentId(comment);
        Article article = articleService.getArticleById(comment.getCommentArticleId());
        article.setArticleOver(1);
        article.setComment(comment);
        //数据更新
        int j = articleService.modifyArticleByArticleId(article);
        if(i==1&&j==1){
            map.put("status", 0);
            return map;
        }
        map.put("status",1 );
        map.put("msg","谁让你采纳的?" );
        return map;
    }
    @ResponseBody
    @RequestMapping(value = "/api/jieda-zan/",method = RequestMethod.POST)
    @Transactional
    public Map great(Integer id ,HttpServletRequest request){
        Map map = new HashMap();
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            map.put("status",1 );
            map.put("msg","未登录" );
            return map;
        }
        //点赞数加一

        return map;
    }
}
